﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="ContentAppBase.View.SectionPage"
    DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:common="using:ContentAppBase.Common"
    xmlns:utils="using:ContentAppBase.Utils"
    mc:Ignorable="d"
    NavigationCacheMode="Enabled">
    
    <Page.Resources>
        <utils:ItemTypeToItemSizeConverter x:Key="ItemTypeToItemSizeConverter" />
    </Page.Resources>

    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Style="{StaticResource PageHeaderTextStyle}"/>
        </Grid>

        <GridView Grid.Row="1" ItemsSource="{Binding CurrentResults}" SelectionMode="None" IsSwipeEnabled="False" IsItemClickEnabled="True" ItemClick="GridView_ItemClick" Padding="120,0,40,46" x:Name="FullscreenList">
            <GridView.ItemTemplateSelector>
                <utils:SectionItemTemplateSelector ImageResultTemplate="{StaticResource ImageResultTemplate}" WebResultTemplate="{StaticResource WebResultTemplate}" />
            </GridView.ItemTemplateSelector>
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VariableSizedWrapGrid ItemWidth="{Binding CurrentResults, Converter={StaticResource ItemTypeToItemSizeConverter}, ConverterParameter=Width}" ItemHeight="{Binding CurrentResults, Converter={StaticResource ItemTypeToItemSizeConverter}, ConverterParameter=Height}" />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
        </GridView>

        <ListView Grid.Row="1" ItemsSource="{Binding CurrentResults}" SelectionMode="None" IsSwipeEnabled="False" IsItemClickEnabled="True" ItemClick="GridView_ItemClick" Padding="20,0" x:Name="SnappedList" Visibility="Collapsed">
            <ListView.ItemTemplateSelector>
                <utils:SectionItemTemplateSelector ImageResultTemplate="{StaticResource ImageResultSnappedTemplate}" WebResultTemplate="{StaticResource WebResultSnappedTemplate}" />
            </ListView.ItemTemplateSelector>
        </ListView>

        <ProgressRing IsActive="{Binding IsLoading}" Grid.Row="1" Width="50" Height="50"/>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="SnappedList">
                        	<DiscreteObjectKeyFrame KeyTime="0">
                        		<DiscreteObjectKeyFrame.Value>
                        			<Visibility>Visible</Visibility>
                        		</DiscreteObjectKeyFrame.Value>
                        	</DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FullscreenList">
                        	<DiscreteObjectKeyFrame KeyTime="0">
                        		<DiscreteObjectKeyFrame.Value>
                        			<Visibility>Collapsed</Visibility>
                        		</DiscreteObjectKeyFrame.Value>
                        	</DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
